# === load relevant libraries
library(data.table)
library(ggplot2)
library(DescTools)
library(plm)
library(lfe)
library(dplyr)
library(sandwich)

rm(list = ls())

# academic style portfolios
data = readRDS('input_data/academic_style_data.RDS')
data = data[, list(yyyymm, category, ret, fit, expSum)]

# create sorting
sortData = data[!is.na(expSum), list(yyyymm, category, expSum)]
sortData = sortData[order(yyyymm, expSum)]
for (this in unique(sortData[, yyyymm])){
  sortData[yyyymm == this, bin := 1:9]
}
rm(this)

# get 36 months of flows and returns after sorting
data[, expSum := NULL]
data = as.data.table(melt(data, id.vars = c('yyyymm','category')))
names(data)[3] = 'var'
tmp = unique(data[, list(yyyymm)])
tmp[, idx := 1:nrow(tmp)]
data = merge(data, tmp); rm(tmp)
out = data.table()
for (i in 0:36){
  out = rbind(out, data[, list(idx = idx-i, hor = i, category, var, value)])
}
rm(i)
out = merge(out, unique(data[, list(idx, yyyymm)]), by = 'idx')
out = merge(out, sortData[, list(yyyymm, category, bin)], by = c('yyyymm','category'), all.x = T)
out = out[0 == rowSums(is.na(out))]
out[, idx := NULL]
data = copy(out); rm(out, sortData)

# summarize
data[, period := ifelse(yyyymm > 200206, 'after 2002', 'before 2002')]
data = data[, list(value = mean(value)), list(bin, hor, var, period)]

# get difference between top and bottom styles
data = merge(data[bin == 9, list(hor, var, period, top = value)], 
             data[bin == 1, list(hor, var, period, bottom = value)], by = c('hor','var', 'period'))
data = data[, list(hor, var, period, value = top - bottom)]
data = data[order(hor)]

# get cumulative values
tmp = unique(data[, list(var, period)])
for (i in 1:nrow(tmp)){
  data[(var == tmp[i, var]) & (period == tmp[i, period]), value := cumsum(value)]
}
rm(i, tmp)

# append pre-computed bootstrap standard errors
tmp = readRDS('input_data/style_price_pressure_bootstraps_academic_style.RDS')
data = merge(data, tmp, by = c('hor','var','period')); rm(tmp)

# plot Figure A6 (a): flow-induced trading
ggplot(data[var == 'fit'], aes(x = hor, y = value, fill = period)) + geom_line(lwd = 1, aes(color = period)) + 
  geom_ribbon(aes(ymin = value - 1.96*se, ymax = value + 1.96*se), alpha = .2) + coord_cartesian(ylim = c(-.05, .2)) + 
  theme_classic() + theme(legend.title = element_blank(), legend.position = c(.2, .8)) + 
  labs(x = 'Months', y = 'Cumulative flow-induced trading: top minus bottom style') + 
  scale_y_continuous(label = scales::percent_format(accuracy = 1)) + geom_hline(yintercept = 0, lty = 3)
  
# plot Figure A6 (b): return
ggplot(data[var == 'ret'], aes(x = hor, y = value, fill = period)) + geom_line(lwd = 1, aes(color = period)) + 
  geom_ribbon(aes(ymin = value - 1.96*se, ymax = value + 1.96*se), alpha = .2) + coord_cartesian(ylim = c(-.1, .15)) + 
  theme_classic() + theme(legend.title = element_blank(), legend.position = c(.2, .2)) + 
  labs(x = 'Months', y = 'Cumulative return: top minus bottom style') + 
  scale_y_continuous(label = scales::percent_format(accuracy = 1)) + geom_hline(yintercept = 0, lty = 3)

